package com.ciwong.epaper.modules.reciteWords.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by keepon on 2018/9/14.
 */
public abstract class BaseRecyclerViewAdapter<T,VH extends RecyclerView.ViewHolder>  extends RecyclerView.Adapter<VH>{
	protected Context mContext;
	protected List<T> mDatas;
	public BaseRecyclerViewAdapter(Context context) {
		mContext = context;
	}
	public BaseRecyclerViewAdapter(Context context,List<T> datas) {
		mContext = context;
		if(datas==null){
			mDatas = new ArrayList<>();
		}
		mDatas = datas;
	}

	@NonNull
	@Override
	public final VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

		return onCreateMyViewHolder(parent,viewType);
	}

	@Override
	public  final void onBindViewHolder(@NonNull VH holder, int position) {
		onBindMyViewHolder(holder, position);
	}

	@Override
	public  int getItemCount() {
		return mDatas!=null?mDatas.size():0;
	}

	public void updateDatas(List<T>  datas) {
		Log.e("TAG", "updateDatas: datas =="+Integer.toHexString(datas.hashCode()) +"  mDatas=="+Integer.toHexString(mDatas.hashCode()));
		if(mDatas!=null){
			mDatas.clear();
			mDatas.addAll(datas);
			notifyDataSetChanged();
		}
	}

	public abstract VH onCreateMyViewHolder(@NonNull ViewGroup parent, int viewType) ;
	public abstract void onBindMyViewHolder(@NonNull VH holder, int position);

	public void setOnItemClickListener(BaseRecyclerViewAdapter.OnItemClickListener<T> onItemClickListener) {
		mOnItemClickListener = onItemClickListener;
	}

	public BaseRecyclerViewAdapter.OnItemClickListener<T> mOnItemClickListener;
	public    interface OnItemClickListener<T>{
		void  onItemClick(int positon, T t);
	}



}
